package com.atguigu.spzx.user.service.imp;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.spzx.common.exception.SPZXException;
import com.atguigu.spzx.model.dto.h5.UserLoginDto;
import com.atguigu.spzx.model.dto.h5.UserRegisterDto;
import com.atguigu.spzx.model.entity.user.UserInfo;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.h5.UserInfoVo;
import com.atguigu.spzx.user.mapper.UserInfoMapper;
import com.atguigu.spzx.user.service.UserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class UserInfoServiceImp implements UserInfoService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private UserInfoMapper userInfoMapper;


    @Override
    public void register(UserRegisterDto userRegisterDto){
        String username = userRegisterDto.getUsername();
        String code = userRegisterDto.getCode();
        String password = userRegisterDto.getPassword();
        String nickName = userRegisterDto.getNickName();
        if(StrUtil.isEmpty(username)||StrUtil.isEmpty(code)||StrUtil.isEmpty(password)||StrUtil.isEmpty(nickName)){
            throw new SPZXException(ResultCodeEnum.DATA_ERROR);
        }
        //校验校验验证码
        String codeValueRedis = redisTemplate.opsForValue().get("phone:code:" + username);
        if(!code.equals(codeValueRedis)){
            throw new SPZXException(ResultCodeEnum.DATA_ERROR);
        }
        UserInfo userInfo = userInfoMapper.getByUsername(username);
         if(userInfo != null){
             throw new SPZXException(ResultCodeEnum.DATA_ERROR);
         }
        //保存用户信息
        userInfo = new UserInfo();
        userInfo.setUsername(username);
        userInfo.setNickName(nickName);
        userInfo.setPassword(DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8)));
        userInfo.setStatus(1);
        userInfo.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        userInfo.setSex(0);
        userInfo.setPhone(username);

        userInfoMapper.insert(userInfo);

        //TODO
        //redisTemplate.delete("phone:code:"+username);

    }

    @Override
    public String login(UserLoginDto userLoginDto, String ip) {
        String username = userLoginDto.getUsername();
        String password = userLoginDto.getPassword();

        //校验参数
        if(StrUtil.isEmpty(username)||StrUtil.isEmpty(password)){
            throw new SPZXException(ResultCodeEnum.DATA_ERROR);
        }
        UserInfo userInfo = userInfoMapper.getByUsername(username);
        if(null == userInfo) {
            throw new SPZXException(ResultCodeEnum.LOGIN_ERROR);
        }
        //校验密码
        String md5InputPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!md5InputPassword.equals(userInfo.getPassword())) {
            throw new SPZXException(ResultCodeEnum.LOGIN_ERROR);
        }
        //校验是否被禁用
        if(userInfo.getStatus() == 0) {
            throw new SPZXException(ResultCodeEnum.ACCOUNT_STOP);
        }
        //更新登录信息
        userInfo.setLastLoginIp(ip);
        userInfo.setLastLoginTime(new Date());
        userInfoMapper.updateById(userInfo);
        String token = UUID.randomUUID().toString().replaceAll("-","");
        redisTemplate.opsForValue().set("user:login:"+token, JSON.toJSONString(userInfo), 30, TimeUnit.DAYS);
        return token;


    }

    @Override
    public UserInfoVo getCurrentUserInfo(String token) {
        String userInfoJSON = redisTemplate.opsForValue().get("user:login:" + token);
        if(StrUtil.isEmpty(userInfoJSON)){
            throw new SPZXException(ResultCodeEnum.LOGIN_AUTH);
        }
        UserInfo userInfo = JSON.parseObject(userInfoJSON, UserInfo.class);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);
        return userInfoVo;
    }

}
